//
//  LZPopAlertView.m
//  PopTest
//
//  Created by xbnzd on 17/2/14.
//  Copyright © 2017年 xbnzd. All rights reserved.
//

#import "LZPopAlertView.h"
#import "Masonry.h"
#import <QuartzCore/QuartzCore.h>


#define kDefaultHeight  30
#define kArrorHeight    4

@interface LZPopAlertView()
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (nonatomic, assign) CGFloat anchorX;

@end

@implementation LZPopAlertView

- (void)loadText:(NSString *)text anchor:(CGPoint)anchorPoint
{
    self.contentLabel.text = text;
    
    CGFloat selfWidth = [self getBgViewWidth];
    CGFloat rightBorderSpace;
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        rightBorderSpace = anchorPoint.x;
    }
    else {
        rightBorderSpace = SCREEN_WIDTH - anchorPoint.x;
    }
    
    if (rightBorderSpace < (selfWidth/2 + 10)) {
        
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            _anchorX = rightBorderSpace - 10;
        }
        else {
            _anchorX = (selfWidth/2 + 10 - rightBorderSpace) + selfWidth/2;
        }
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(SCREEN_WIDTH - selfWidth - 10);
            make.top.mas_equalTo(anchorPoint.y - kDefaultHeight);
            make.size.mas_equalTo(CGSizeMake(selfWidth, kDefaultHeight));
        }];
    }else{
        _anchorX = selfWidth/2;
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(anchorPoint.x - selfWidth/2);
            make.top.mas_equalTo(anchorPoint.y - kDefaultHeight);
            make.size.mas_equalTo(CGSizeMake(selfWidth, kDefaultHeight));
        }];
    }
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
     [self drawInContext:UIGraphicsGetCurrentContext()];
    
}

- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 2.0);
    
    CGContextSetFillColorWithColor(context, [UIColorFromRGB(0x33a928, 1) CGColor]);
    
    
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, _anchorX+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,_anchorX, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,_anchorX-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, _anchorX, maxy, radius);
    CGContextClosePath(context);
}


- (CGFloat)getBgViewWidth
{
    return [_contentLabel intrinsicContentSize].width + 16;
}

@end
